package com.neusoft.yiyang.diet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.yiyang.common.entity.diet.DietTag;
import com.neusoft.yiyang.common.entity.diet.FoodDict;
import com.neusoft.yiyang.common.entity.diet.FoodTag;
import com.neusoft.yiyang.common.entity.diet.vto.FoodDictWithTag;
import com.neusoft.yiyang.diet.mapper.DietTagMapper;
import com.neusoft.yiyang.diet.mapper.FoodDictMapper;
import com.neusoft.yiyang.diet.mapper.FoodTagMapper;
import com.neusoft.yiyang.diet.service.FoodDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FoodDictServiceImpl implements FoodDictService {

    @Autowired
    private FoodDictMapper foodDictMapper;

    @Autowired
    private DietTagMapper dietTagMapper;

    @Autowired
    private FoodTagMapper foodTagMapper; // 注入 FoodTagMapper

    @Override
    public void addFoodDict(FoodDict foodDict) {
        // 插入 FoodDict 记录
        foodDictMapper.insert(foodDict);
        // 获取插入后的 foodId
        Integer foodId = foodDict.getFoodId();

        List<String> tags = new ArrayList<>();
        if (foodDict.getTags() != null) {
            for (DietTag tag : foodDict.getTags()) {
                tags.add(tag.getTagName());
            }
        }
        List<Integer> tagIds = new ArrayList<>();
        if (tags != null &&!tags.isEmpty()) {
            // 获取 tagIds
            tagIds = dietTagMapper.selectByTagNames(tags);
        }

        // 构建 FoodTag 并插入
        if (tagIds != null && !tagIds.isEmpty()) {
            for (Integer tagId : tagIds) {
                FoodTag foodTag = new FoodTag();
                foodTag.setFoodId(foodId);
                foodTag.setTagId(tagId);
                // 插入 FoodTag 记录
                foodTagMapper.insert(foodTag);
            }
        }
    }


    @Override
    public List<FoodDict> getAllFoodDicts() {
        return foodDictMapper.selectList(null);
    }

    @Override
    public List<FoodDict> getFoodDictByName(String foodName) {
        return foodDictMapper.selectByFoodName(foodName);
    }

    @Override
    public void updateFoodDict(FoodDict foodDict) {
        foodDictMapper.updateById(foodDict);
        Integer foodId = foodDict.getFoodId();

        // 先获取新传入的标签名称列表
        List<String> newTagNames = new ArrayList<>();
        if (foodDict.getTags() != null && !foodDict.getTags().isEmpty()) {
            for (DietTag tag : foodDict.getTags()) {
                newTagNames.add(tag.getTagName());
            }
        }

        // 根据标签名称列表查询对应的 tagId 列表
        List<Integer> newTagIds = new ArrayList<>();
        if (!newTagNames.isEmpty()) {
            newTagIds = dietTagMapper.selectByTagNames(newTagNames);
        }

        QueryWrapper<FoodTag> oldQueryWrapper = new QueryWrapper<>();
        oldQueryWrapper.eq("food_id", foodId);
        oldQueryWrapper.select("tag_id");
        List<FoodTag> oldFoodTags = foodTagMapper.selectList(oldQueryWrapper);
        List<Integer> oldTagIds = new ArrayList<>();
        for (FoodTag foodTag : oldFoodTags) {
            oldTagIds.add(foodTag.getTagId());
        }

        List<Integer> tagsToAdd = new ArrayList<>(newTagIds);
        tagsToAdd.removeAll(oldTagIds);

        List<Integer> tagsToDelete = new ArrayList<>(oldTagIds);
        tagsToDelete.removeAll(newTagIds);

        // 插入新的标签关联记录
        for (Integer tagId : tagsToAdd) {
            FoodTag foodTag = new FoodTag();
            foodTag.setFoodId(foodId);
            foodTag.setTagId(tagId);
            foodTagMapper.insert(foodTag);
        }

        // 删除不再需要的标签关联记录
        for (Integer tagId : tagsToDelete) {
            QueryWrapper<FoodTag> deleteWrapper = new QueryWrapper<>();
            deleteWrapper.eq("food_id", foodId).eq("tag_id", tagId);
            foodTagMapper.delete(deleteWrapper);
        }
    }

    @Override
    public void deleteFoodDict(Integer foodId) {
        foodDictMapper.deleteById(foodId);
        QueryWrapper<FoodTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("food_id", foodId);
        foodTagMapper.delete(queryWrapper);
    }

    @Override
    public List<FoodDict> listFoodWithTags() {
        return foodDictMapper.listFoodWithTags();
    }

    @Override
    public List<FoodDictWithTag> getFoodWithTagByTag(String tagName) {
        return foodDictMapper.getFoodWithTagByTag(tagName);
    }
}
